import * as types from '../types'
import axios from 'axios'

const state = {
  tags: []
}

const getters = {
  tags: state => state.tags
}

const mutations = {
  [types.TAGS_LOADED](state, tags) {
    state.tags = tags || []
  }
}

const actions = {
  async searchTags({
    commit
  }, query) {
    let response = await axios.post('/vts/tag/query', query)
    commit(types.TAGS_LOADED, response.data)
  },
  async getTagRelations({
    commit
  }, data) {
    let response = await axios.post('/vts/tag/search', data.query)
    data.callback(response.data)
  },
  async batchCreateTags({
    commit
  }, data) {
    let response = await axios.post('/vts/tag/batchCreate', data.model)
    if (response.data.saved) {
      data.callback()
    }
  },
  async reorderTags({
    commit
  }, data) {
    let response = await axios.post('/vts/tag/reorder', data.model)
    if (response.data.saved) {
      data.callback()
    }
  },
  async mergeTags({
    commit
  }, data) {
    let response = await axios.post('/vts/tag/merge', data.model)
    if (response.data.saved) {
      data.callback()
    }
  }
}

export default {
  state,
  mutations,
  getters,
  actions
}
